package fe.bmsearch.gui.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;

public class Shortcuts extends Composite {

    private static ShortcutsUiBinder uiBinder = GWT
            .create(ShortcutsUiBinder.class);

    interface ShortcutsUiBinder extends UiBinder<StackLayoutPanel, Shortcuts> {
    }
    @UiField
    Button searchButton;
    @UiField
    ListBox documentTypeBox;
    @UiField
    ListBox pathBox;
    @UiField
    TextBox selectorBox;
    final List<SearchResultListener> searchResultListeners = new ArrayList<SearchResultListener>();

    public void addSearchResultListener(SearchResultListener l) {
        searchResultListeners.add(l);
    }

    public Shortcuts() {
        initWidget(uiBinder.createAndBindUi(this));
        documentTypeBox.addItem("Email", "Email");
        documentTypeBox.addItem("SMS", "SMS");

        documentTypeBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {

                pathBox.clear();
                PathTypesServiceAsync service = GWT.create(PathTypesService.class);

                final AsyncCallback<List<PathToSelectorItem>> pathTypesCB = new AsyncCallback<List<PathToSelectorItem>>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Error: " + caught.getMessage());
                    }

                    @Override
                    public void onSuccess(List<PathToSelectorItem> paths) {

                        for (PathToSelectorItem l : paths) {
                            pathBox.addItem(l.description, l.unique);
                        }

                    }
                };

                service.getPathToSelectorItems(documentTypeBox.getItemText(documentTypeBox.getSelectedIndex()), pathTypesCB);

            }
        });

        searchButton.setText("Search");

        searchButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {

                BMIndexedSearchServiceAsync service = GWT.create(BMIndexedSearchService.class);

                final AsyncCallback<List<SearchResultItem>> indexedSearchCB = new AsyncCallback<List<SearchResultItem>>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Error: " + caught.getMessage());
                    }

                    @Override
                    public void onSuccess(List<SearchResultItem> result) {

                        for (SearchResultListener l : searchResultListeners) {
                            l.onSearchResult(result);
                        }

                    }
                };
                service.doSearch(documentTypeBox.getItemText(documentTypeBox.getSelectedIndex()),
                        pathBox.getValue(pathBox.getSelectedIndex()),
                        selectorBox.getValue(),
                        indexedSearchCB);
            }
        });



        PathTypesServiceAsync service = GWT.create(PathTypesService.class);

        final AsyncCallback<List<PathToSelectorItem>> pathTypesCB = new AsyncCallback<List<PathToSelectorItem>>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error: " + caught.getMessage());
            }

            @Override
            public void onSuccess(List<PathToSelectorItem> paths) {

                for (PathToSelectorItem l : paths) {
                    pathBox.addItem(l.description, l.unique);
                }

            }
        };

        service.getPathToSelectorItems(documentTypeBox.getItemText(documentTypeBox.getSelectedIndex()), pathTypesCB);


    }
}
